package ViewOption;

import Pojo.User;
import Service.CreditCardService;
import Utils.BaseMessage;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class creditCardVo {
    private final CreditCardService creditCardService = new CreditCardService();
    private final Logger logger = Logger.getLogger(creditCardVo.class);

    /**
     * 根据用户ID插入信用卡信息
     *
     * @param request
     * @return
     */
    public Object insertCreditCardByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardNumId = request.getParameter("cardNumId");
        String cardPrice = request.getParameter("cardPrice");
        String cardTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(request.getParameter("cardTime")));
        String bankId = request.getParameter("bankId");
        try {
            if (creditCardService.insertCreditCardByUserId(cardNumId, cardPrice, cardTime, bankId, userId) < 1) {
                return new BaseMessage(2, "出现错误，请联系管理员", null);
            }
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常，请联系管理员处理", null);
        }
        return new BaseMessage(0, null, "增加成功");
    }

    /**
     * 获取所有未还款的
     *
     * @param request
     * @return
     */
    public Object getCreditCardNoRePayByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardNum = request.getParameter("cardNum");
        String pageIndex = request.getParameter("pageIndex");
        Map<String, Object> data = new HashMap<>();
        try {
            String cardNum1 = cardNum == null || cardNum.length() == 0 ? "%%" : "%" + cardNum + "%";
            data.put("data", creditCardService.getCreditCardNoRePayByUserIdAndIndex(userId, cardNum1, ((Integer.parseInt(pageIndex) - 1) * 10)));
            data.put("itemsCount", creditCardService.getCreditCardNoRePayByUserIdAndIndex(userId, cardNum1));
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常请联系管理员", null);
        }
        return new BaseMessage(0, null, data);

    }

    /**
     * 修改信用卡的状态
     *
     * @param request
     * @return
     */
    public Object updateCreditCardState(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardId = request.getParameter("cardId");
        try {
            if (creditCardService.updateCardStateByUserIdAndCardId(userId, cardId) < 1) {
                return new BaseMessage(2, "修改失败，请联系管理员", null);
            }
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常,请联系管理员处理", null);
        }
        return new BaseMessage(0, null, "还款成功");
    }

    /**
     * 根据条件获取
     *
     * @param request
     * @return
     */
    public Object getManagerCreditCardAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String minTime = request.getParameter("minTime");
        String maxTime = request.getParameter("maxTime");
        String bankId = request.getParameter("bankId");
        String isPay = request.getParameter("isPay");
        String cardNum = request.getParameter("cardNum");
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Map<String, Object> data = new HashMap<>();
        try {
            data.put("data", creditCardService.getManagerCardBySeletive(userId, minTime, maxTime, bankId, isPay, cardNum, (pageIndex - 1) * 10));
            data.put("itemsCount", creditCardService.getManagerCardCountBySeletive(userId, minTime, maxTime, bankId, isPay, cardNum));
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常，请联系管理员", null);
        }
        return new BaseMessage(0, null, data);
    }

    /**
     * 根据信用卡ID获取信用卡信息
     *
     * @param request
     * @return
     */
    public Object getCreditCardByCardId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardId = request.getParameter("cardId");
        try {
            return new BaseMessage(0, null, creditCardService.getCreditCardByUserAndId(userId, cardId));
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常,请联系管理员查看", null);
        }
    }

    /**
     * 修改信用卡信息
     *
     * @param request
     * @return
     */
    public Object updateCreditCardMsgByCardId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardPrice = request.getParameter("cardPrice");
        String cardTime = request.getParameter("cardTime");
        String bankId = request.getParameter("bankId");
        String cardState = request.getParameter("cardState");
        String cardId = request.getParameter("cardId");
        try {
            if (creditCardService.updateCreditCardMsgByUserIDAndCardId(cardPrice, cardTime, cardState, bankId, userId, cardId) < 1) {
                return new BaseMessage(2, "出现错误，请联系管理员", null);
            }
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常,请联系管理员", null);
        }
        return new BaseMessage(0, null, "修改成功");
    }

    /**
     * 删除信用卡
     *
     * @param request
     * @return
     */
    public Object deleteUserCreditCardById(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("userMsg");
        int userId = user.getUserId();
        String cardId = request.getParameter("cardId");
        try {
            if (creditCardService.deleteUserCreditCardById(userId, cardId) < 1) {
                return new BaseMessage(2, "删除失败,请联系管理员处理", null);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return new BaseMessage(3, "删除异常,请联系管理员查看", null);
        }
        return new BaseMessage(0, null, "删除成功");
    }

    public Object getCreditCardAllIsAdmin(HttpServletRequest request) {
        String minTime = request.getParameter("minTime");
        String maxTime = request.getParameter("maxTime");
        String bankId = request.getParameter("bankId");
        String cardState = request.getParameter("cardState");
        String searchCon = request.getParameter("searchCon");
        String search = request.getParameter("search");
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));

        Map<String, Object> data = new HashMap<>();
        try {
            data.put("data", creditCardService.getCreditCardAllIsAdmin(minTime, maxTime, bankId, cardState, searchCon, search, (pageIndex - 1) * 10));
            data.put("itemsCount", creditCardService.getCreditCardAllCountIsAdmin(minTime, maxTime, bankId, cardState, searchCon, search));
        } catch (SQLException throwables) {
            logger.error(throwables.getMessage());
            return new BaseMessage(3, "出现异常,请联系管理员处理", null);
        }
        return new BaseMessage(0, null, data);

    }
}
